﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Windvoice.Blog;

namespace Windvoice.Areas.Manage.Controllers
{
    public class LinkController : ManagerControllerBase
    {
        //
        // GET: /Manage/Links/

        public ActionResult Index(int? page)
        {
            int PageIndex = page == null ? 1 : page.Value;
            int PageSize = 20;
            int RecordCount = 0;
            ViewBag.LinkList = Link.GetList(PageIndex, PageSize, true, out RecordCount);
            ViewBag.RecordCount = RecordCount;
            ViewBag.PageSize = PageSize;

            return View();
        }

        public ActionResult Candidate(int? page)
        {
            int PageIndex = page == null ? 1 : page.Value;
            int PageSize = 20;
            int RecordCount = 0;
            ViewBag.LinkList = Link.GetList(PageIndex, PageSize, false, out RecordCount);
            ViewBag.RecordCount = RecordCount;
            ViewBag.PageSize = PageSize;

            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Link link)
        {
            link.CreateTime = DateTime.Now;

            if (ModelState.IsValid)
            {
                link.Create();
                return RedirectToAction("index");
            }
            return View();
        }


        public ActionResult Edit(int id)
        {
            var link = Link.Find(id);
            return View(link);
        }

        [HttpPost]
        public ActionResult Edit(Link link)
        {
            link.CreateTime = Link.Find(link.ID).CreateTime;

            if (ModelState.IsValid)
            {
                link.Update();
                return RedirectToAction("index");
            }
            return View(link);
        }

        [HttpPost]
        public ActionResult Delete(FormCollection formValues)
        {
            if (!String.IsNullOrEmpty(formValues["id"]))
            {
                var LinkID = Convert.ToInt32(formValues["id"]);
                Link.Find(LinkID).Delete();
                return Json(new { result = true, message = String.Empty });
            }
            return Json(new { result = false, message = "参数错误" });
        }

        [HttpPost]
        public ActionResult ChangeStatus(FormCollection formValues)
        {
            if (!String.IsNullOrEmpty(formValues["id"]) && !String.IsNullOrEmpty(formValues["isenable"]))
            {
                var link = Link.Find(Convert.ToInt32(formValues["id"]));

                if (link != null)
                {
                    link.IsEnable = Convert.ToInt32(formValues["isenable"]) == 1 ? true : false;
                    link.Save();

                    return AjaxResult();
                }
                else
                {
                    return AjaxResult(result: false, message: "未找到对象");
                }
            }
            else
            {
                return AjaxResult(result: false, message: "参数错误");
            }
        }
    }
}